package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.R;
import com.sgb.kjwl.utils.TabLayoutUtils;

public class SearchView extends RelativeLayout {

    private EditText mEditSearch;
    private ImageView mIvClose;

    public SearchView(Context context) {
        super(context);
        initView(context,null);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    private void initView(Context context,AttributeSet attrs){
        if (attrs != null) {
            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.SearchViewYoung, 0, 0);
            try {
                LayoutInflater.from(context).inflate(R.layout.view_search, this);

                String hint = attributes.getString(R.styleable.SearchViewYoung_hintForSearchView);

                mEditSearch = findViewById(R.id.edit_search);
                mIvClose = findViewById(R.id.iv_close);

                mEditSearch.setHint(hint);

                mIvClose.setVisibility(GONE);
                mEditSearch.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (mEditSearch.getText().toString().length()>0){
                            mIvClose.setVisibility(VISIBLE);
                        }else {
                            mIvClose.setVisibility(GONE);
                        }
                    }
                });

                mIvClose.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditSearch.setText("");
                        mIvClose.setVisibility(GONE);
                    }
                });
            } finally {
                attributes.recycle();
            }
        }
    }

    public String getEditContent(){
        return mEditSearch.getText().toString();
    }
}
